package com.hellobike.haxing.modules.order.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.hellobike.haxing.common.vo.Result;
import com.hellobike.haxing.common.system.query.QueryGenerator;
import com.hellobike.haxing.common.util.oConvertUtils;
import com.hellobike.haxing.modules.order.constant.OrderConstants;
import com.hellobike.haxing.modules.order.entity.OrderRefund;
import com.hellobike.haxing.modules.order.service.IOrderRefundService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import com.hellobike.haxing.common.system.base.controller.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import com.hellobike.haxing.common.annotation.AutoLog;
 /**
 * @Description: 订单退款记录
 * @Author: haxing-boot
 * @Date:   2020-02-08
 * @Version: V1.0
 */
@RestController
@RequestMapping("/order/orderRefund")
@Slf4j
public class OrderRefundController extends BaseController<OrderRefund, IOrderRefundService> {
	@Autowired
	private IOrderRefundService orderRefundService;
	
	/**
	 * 分页列表查询
	 *
	 * @param orderRefund
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "订单退款记录-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(OrderRefund orderRefund,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<OrderRefund> queryWrapper = QueryGenerator.initQueryWrapper(orderRefund, req.getParameterMap());
		Page<OrderRefund> page = new Page<OrderRefund>(pageNo, pageSize);
		IPage<OrderRefund> pageList = orderRefundService.queryPage(page, queryWrapper);
		return Result.ok(pageList);
	}
	
	/**
	 *   添加
	 *
	 * @param orderRefund
	 * @return
	 */
	@AutoLog(value = "订单退款记录-添加",operateType=2)
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody OrderRefund orderRefund) {
		orderRefundService.save(orderRefund);
		return Result.ok("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param orderRefund
	 * @return
	 */
	@AutoLog(value = "订单退款记录-编辑",operateType=3)
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody OrderRefund orderRefund) {
		orderRefundService.updateById(orderRefund);
		return Result.ok("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "订单退款记录-删除",operateType=4)
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) Long id) {
		orderRefundService.removeById(id);
		return Result.ok("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "订单退款记录-批量删除",operateType=4)
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.orderRefundService.removeByIds(Arrays.asList(ids.split(",")).stream().map(s -> Long.parseLong(s.trim())).collect(Collectors.toList()));
		return Result.ok("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) Long id) {
		OrderRefund orderRefund = orderRefundService.getById(id);
		if(orderRefund==null) {
			return Result.error("未找到对应数据");
		}
		return Result.ok(orderRefund);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param orderRefund
    */
    @AutoLog(value = "订单退款记录-导出excel",operateType=6)
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, OrderRefund orderRefund) {
        return super.exportXls(request, orderRefund, OrderRefund.class, "订单退款记录");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @AutoLog(value = "订单退款记录-导入excel",operateType=5)
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, OrderRefund.class);
    }

	 /**
	  * 再次退款
	  * @param id
	  * @return
	  */
	 @AutoLog(value = "订单退款记录-再次退款",operateType=14)
	 @GetMapping(value = "/updateRefund")
	 public Result<?> updateRefund(@RequestParam(name="id",required=true) Long id) {
		 return orderRefundService.updateRefund(id);
	 }

}
